package com.lottery.material.task;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lottery.material.common.Constats;
import com.lottery.material.entity.article.CompetitionInfo;
import com.lottery.material.service.portal.competition.CompetitionInfoService;
import com.lottery.material.utils.CalendarUtil;
import com.lottery.material.utils.EmptyUtil;
import com.lottery.material.utils.HttpClientUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author:houjie
 * @Description: 获取赛事列表定时任务
 * @Date:Create in 2019/4/10 14:46
 */
@Slf4j
@Component
public class CompetitionTask {

    @Autowired
    CompetitionInfoService competitionInfoService;

    //@Scheduled(cron = "* 0/5 * * * ?")
    public void updateCompetitionData() {
        log.info("更新赛事数据开始。。。。。");
        //获取足球的数据
        List<CompetitionInfo> ZQList = getCompetitionList(Constats.COMPETITION_TYPE_ZQ);
        if(EmptyUtil.isNotEmpty(ZQList) && ZQList.size()>0){
            for(int i=0;i<ZQList.size();i++){
                CompetitionInfo competitionInfo=competitionInfoService.findByCompetitionTypeAndOpenId(1,ZQList.get(i).getOpenId());
                if(EmptyUtil.isNotEmpty(competitionInfo)){//存在判断状态
                    if(competitionInfo.getState()==1 && ZQList.get(i).getState()==2){
                        competitionInfo.setState(2);
                        competitionInfo.setScore(ZQList.get(i).getScore());
                        competitionInfoService.update(competitionInfo);
                    }
                }else{
                    competitionInfoService.save(ZQList.get(i));
                }
            }
        }
        //获取篮球的数据
        List<CompetitionInfo> LQList = getCompetitionList(Constats.COMPETITION_TYPE_LQ);
        if(EmptyUtil.isNotEmpty(LQList) && LQList.size()>0){
            for(int i=0;i<LQList.size();i++){
                CompetitionInfo competitionInfo=competitionInfoService.findByCompetitionTypeAndOpenId(2,LQList.get(i).getOpenId());
                if(EmptyUtil.isNotEmpty(competitionInfo)){//存在判断状态
                    if(competitionInfo.getState()==1 && LQList.get(i).getState()==2){
                        competitionInfo.setState(2);
                        competitionInfo.setScore(LQList.get(i).getScore());
                        competitionInfoService.update(competitionInfo);
                    }
                }else{
                    competitionInfoService.save(LQList.get(i));
                }
            }
        }
        log.info("更新赛事数据结束。。。。。");
    }

    private List<CompetitionInfo> getCompetitionList(String type){
        String result = null;
        try {
            result= HttpClientUtil.doGet(Constats.COMPETITION_URL+"?type="+type);
            List<CompetitionInfo> list = new ArrayList<CompetitionInfo>();
            if(EmptyUtil.isNotEmpty(result)){
                JSONObject resultJsonObject=JSONObject.parseObject(result);
                if("1".equals(resultJsonObject.getString("status"))){
                    JSONArray array=resultJsonObject.getJSONArray("data");
                    if(EmptyUtil.isNotEmpty(array) && array.size()>0){
                        for(int i=0;i<array.size();i++){
                            JSONObject json = array.getJSONObject(i);
                            CompetitionInfo competitionInfo=new CompetitionInfo();
                            competitionInfo.setCompetitionType(Constats.COMPETITION_TYPE_ZQ.equals(type)?1:2);
                            competitionInfo.setHomeTeam(json.getString("homesxname"));//主队简称
                            competitionInfo.setVisitingTeam(json.getString("awaysxname"));//客队简称
                            competitionInfo.setLeague(json.getString("simpleleague"));//联赛名称简称
                            competitionInfo.setOpenId(json.getString("pid"));//比赛唯一编号
                            competitionInfo.setPlayingDate(CalendarUtil.dateToString(CalendarUtil.stringToDate(json.getString("mtime"),"yyyy-MM-dd HH:mm:ss"),"yyyy-MM-dd"));//比赛日期
                            competitionInfo.setPlayingTime(CalendarUtil.dateToString(CalendarUtil.stringToDate(json.getString("mtime"),"yyyy-MM-dd HH:mm:ss"),"HH:mm:ss"));//比赛时间
                            if(EmptyUtil.isNotEmpty(json.getString("score"))){
                                competitionInfo.setScore(json.getString("score"));//比分
                                competitionInfo.setState(2);
                            }else{
                                competitionInfo.setState(1);
                            }
                            list.add(competitionInfo);
                        }
                    }
                }
            }
            return list;
        } catch (Exception e) {
            log.error("赛事获取接口出错！");
            return null;
        }
    }
}
